﻿using IOC.Desgin;
using ORM.DB;
using ORM.Enum;
using ORM.Model;
using ORM.Util;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace ORM.Base
{
    public class Command
    {
        private string _ConnectStringName;
        public string ConnectStringName
        {
            set
            {
                _ConnectStringName = value;
                _DBHelper = new DBHelper(ConnectStringName);
            }
            get
            {
                return _ConnectStringName;
            }
        }
        public string GlobalTransitionID { set; get; }
        public string OptionID { set; get; }
        public CommandOptionType CommandType { set; get; }
        public string SQL { set; get; }
        public List<Param> Params { set; get; }
        public DbTransaction Transition { set; get; }
        public bool TraceSQL { set; get; }
        private DBHelper _DBHelper { set; get; }
      
        public Command()
        {
            OptionID = ConfigUtil.GetUUID();
            TraceSQL = false;
            _DBHelper = new DBHelper();
        }

        public Command(string ConnectStringName)
        {
            OptionID = ConfigUtil.GetUUID();
            TraceSQL = false;
            this.ConnectStringName = ConnectStringName;
        }
        [InterceptAttibute]
        public virtual void Execute()
        {
            if (!string.IsNullOrEmpty(SQL))
            {
                if (Transition != null)
                {
                    _DBHelper.ExecuteSQL(Transition, SQL, Params);
                }
                else
                {
                    _DBHelper.ExecuteSQL(SQL, Params);
                }
            }


        }
        [InterceptAttibute]
        public virtual Object ExecuteForResult()
        {
            return _DBHelper.ExecuteScar(SQL, Params);

        }
        [InterceptAttibute]
        public virtual List<T> Query<T>() where T : BaseEntity
        {
            List<T> list = new List<T>();
            DataTable dt = _DBHelper.ExecuteQuery(SQL, Params);
            foreach (DataRow item in dt.Rows)
            {
                var Entity = MapperBuilder.GetInstance().MapData2Entity<T>(item);
                list.Add(Entity);
            }
            return list;

        }
        [InterceptAttibute]
        public virtual DataTable QueryView()
        {
            DataTable dt = _DBHelper.ExecuteQuery(SQL, Params);
            return dt;
        }

        public string GetRealSQLText()
        {
            string RealSQLText = this.SQL;
            foreach (Param item in Params)
            {
                RealSQLText = RealSQLText.Replace("@" + item.ParamName, "'" + item.ParamValue + "'");
            }
            return RealSQLText;
        }

        public void Trace(int TotalMilliseconds)
        {
            LogUtil.WriteLog("ORMTrace", "CommandTraceLog", "执行时间：" + TotalMilliseconds + Environment.NewLine);
            LogUtil.WriteLog("ORMTrace", "CommandTraceLog", "sql语句：" + SQL + Environment.NewLine);
            string paramsstr = string.Empty;
            foreach (Param item in Params)
            {
                paramsstr += "name：" + item.ParamName + "/value：" + Convert.ToString(item.ParamValue) + Environment.NewLine;
            }
            LogUtil.WriteLog("ORMTrace", "CommandTraceLog", "sql语句对应参数：" + paramsstr);
            LogUtil.WriteLog("ORMTrace", "CommandTraceLog", "====================================================================" + Environment.NewLine);
        }

        public void TraceOverTime(int TotalMilliseconds)
        {
            LogUtil.WriteLog("ORMTrace", "OverTimeTraceLog", "执行时间：" + TotalMilliseconds + Environment.NewLine);
            LogUtil.WriteLog("ORMTrace", "OverTimeTraceLog", "拦截来源：Command,sql语句：" + SQL + Environment.NewLine);
            string paramsstr = string.Empty;
            foreach (Param item in Params)
            {
                paramsstr += "name：" + item.ParamName + "/value：" + Convert.ToString(item.ParamValue) + Environment.NewLine;
            }
            LogUtil.WriteLog("ORMTrace", "OverTimeTraceLog", "sql语句对应参数：" + paramsstr);
            LogUtil.WriteLog("ORMTrace", "OverTimeTraceLog", "====================================================================" + Environment.NewLine);
        }
    }
}
